package com.itheima;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket implements Runnable{
    private int ticket = 100;
    Lock lock = new ReentrantLock();
    //买票

    @Override
    public void run() {
        while (true){
            lock.lock();
            if(ticket>0){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //获取当前线程对象的方法
                String name = Thread.currentThread().getName();
                System.out.println(name+"正在卖:"+ticket--);
            }
            lock.unlock();
        }
    }
}
public class testBF {
    public static void main(String[] args) {
       //创建线程任务对象
        Ticket ticket = new Ticket();
        //创建三分窗口的对象
        Thread t1 = new Thread(ticket,"窗口一");
        Thread t2 = new Thread(ticket,"窗口二");
        Thread t3 = new Thread(ticket,"窗口三");
        //同时买票

        t1.start();
        t2.start();
        t3.start();
    }
}
